package com.ruoyi.applet.service.impl;

import com.ruoyi.applet.OutDTO.CustomsPassDTO;
import com.ruoyi.applet.domain.AppletSelect;
import com.ruoyi.applet.domain.AppletSite;
import com.ruoyi.applet.domain.WechatUser;
import com.ruoyi.applet.mapper.AppletExamMapper;
import com.ruoyi.applet.mapper.AppletSiteMapper;
import com.ruoyi.applet.mapper.WechatUserMapper;
import com.ruoyi.applet.service.IAppletSiteService;
import com.brisk.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * appletSiteService业务层处理
 *
 * @author PENGFEIDENG
 * @date 2022-05-30
 */
@Service
public class AppletSiteServiceImpl implements IAppletSiteService {
    @Autowired
    private AppletSiteMapper appletSiteMapper;

    @Autowired
    private AppletExamMapper appletExamMapper;

    @Autowired
    private WechatUserMapper wechatUserMapper;

    @Override
    public Map<String, Object> getSiteInfoCharPie() {
        //查询所有点位信息
        List<AppletSite> siteList = appletSiteMapper.selectAppletSiteList(new AppletSite());

        List<String> legendData = new ArrayList<>();
        List<Map<String, Object>> seriesData = new ArrayList<>();
        for (AppletSite appletSite : siteList) {
            legendData.add(appletSite.getSiteName());
            int siteSum = wechatUserMapper.getSitePeopleSum(appletSite.getSiteLockMileage());
            Map<String, Object> siteInfo = new HashMap<>();
            siteInfo.put("mileage", appletSite.getSiteLockMileage());
            siteInfo.put("name", appletSite.getSiteName());
            siteInfo.put("value", siteSum);
            seriesData.add(siteInfo);
        }
        Map<String, Object> res = new HashMap<>();
        res.put("legendData", legendData);
        res.put("seriesData", seriesData);
        return res;
    }

    /**
     * 查询appletSite
     *
     * @param siteId appletSite主键
     * @return appletSite
     */
    @Override
    public AppletSite selectAppletSiteBySiteId(Long siteId) {
        AppletSite appletSite = appletSiteMapper.selectAppletSiteBySiteId(siteId);
        if (!ObjectUtils.isEmpty(appletSite)) {
            int siteSum = wechatUserMapper.getSitePeopleSum(appletSite.getSiteLockMileage());
            appletSite.setPeopleSum(siteSum);
        }
        return appletSite;
    }

    /**
     * 查询appletSite列表
     *
     * @param appletSite appletSite
     * @return appletSite
     */
    @Override
    public List<AppletSite> selectAppletSiteList(AppletSite appletSite) {
        List<AppletSite> siteList = appletSiteMapper.selectAppletSiteList(appletSite);
        if (siteList.size() > 0) {
            for (AppletSite site : siteList) {
                int siteSum = wechatUserMapper.getSitePeopleSum(site.getSiteLockMileage());
                site.setPeopleSum(siteSum);
            }
        }
        return siteList;
    }

    /**
     * 新增appletSite
     *
     * @param appletSite appletSite
     * @return 结果
     */
    @Override
    public int insertAppletSite(AppletSite appletSite) {
        appletSite.setCreateTime(DateUtils.getNowDate());
        return appletSiteMapper.insertAppletSite(appletSite);
    }

    /**
     * 修改appletSite
     *
     * @param appletSite appletSite
     * @return 结果
     */
    @Override
    public int updateAppletSite(AppletSite appletSite) {


        appletSite.setUpdateTime(DateUtils.getNowDate());
        return appletSiteMapper.updateAppletSite(appletSite);
    }

    /**
     * 批量删除appletSite
     *
     * @param siteIds 需要删除的appletSite主键
     * @return 结果
     */
    @Override
    public int deleteAppletSiteBySiteIds(Long[] siteIds) {
        return appletSiteMapper.deleteAppletSiteBySiteIds(siteIds);
    }

    /**
     * 删除appletSite信息
     *
     * @param siteId appletSite主键
     * @return 结果
     */
    @Override
    public int deleteAppletSiteBySiteId(Long siteId) {
        return appletSiteMapper.deleteAppletSiteBySiteId(siteId);
    }

    @Override
    public List<AppletSelect> getSiteSelectList() {
        return appletSiteMapper.getSiteSelectList();
    }

    @Override
    public List<CustomsPassDTO> getCustomsPassList(Long userId, List<AppletSite> siteList) {
        List<CustomsPassDTO> customsPassDTOList = new ArrayList<>();
        if (null == userId) return customsPassDTOList; //获取用户信息
        WechatUser wechatUser = wechatUserMapper.selectWechatUserByUserId(userId);
        if (ObjectUtils.isEmpty(wechatUser)) return customsPassDTOList;
        //获取点位的信息
        if (siteList.size() > 0) {
            for (AppletSite appletSite : siteList) {
                CustomsPassDTO customsPassDTO = new CustomsPassDTO();
                customsPassDTO.setUserId(wechatUser.getUserId());
                customsPassDTO.setSiteId(appletSite.getSiteId());
                customsPassDTO.setSiteName(appletSite.getSiteName());
                customsPassDTO.setMileage(appletSite.getSiteLockMileage());
                if (appletSite.getSiteLockMileage().compareTo(wechatUser.getMileage()) < 1) {
                    customsPassDTO.setCustomsPssImg(appletSite.getSiteYesLock());
                    customsPassDTO.setIsLock(true);
                } else {
                    customsPassDTO.setCustomsPssImg(appletSite.getSiteNoLock());
                    customsPassDTO.setIsLock(false);
                }
                customsPassDTOList.add(customsPassDTO);
            }
        }

        //重新按解锁里程排序
        customsPassDTOList = customsPassDTOList.stream().sorted(Comparator.comparing(CustomsPassDTO::getMileage)).collect(Collectors.toList());
        return customsPassDTOList;
    }
}
